package com.leyou.order.service.controller;

import com.leyou.common.bean.PageResult;
import com.leyou.common.bean.Result;
import com.leyou.order.inter.dto.OrderPageDTO;
import com.leyou.order.inter.pojo.Order;
import com.leyou.order.service.service.OrderService;
import com.leyou.order.service.utils.PayHelper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author zqq
 * @ClassName OrderController
 * @Description: 订单服务接口
 * @date 2020/3/10-17:45
 */
@Api(value = "OrderController",description = "订单服务接口")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayHelper payHelper;

    @PostMapping("add")
    @ApiOperation(value = "创建订单接口，返回订单编号", notes = "创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token"),
            @ApiImplicitParam(name = "order", required = true, value = "订单的json对象,包含订单条目和物流信息")})
    public Result<String> createOrder(@RequestBody @Valid Order order) {
        return this.orderService.createOrder(order);
    }

    @GetMapping("query/{id}")
    @ApiOperation(value = "根据订单编号查询订单，返回订单对象", notes = "查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token"),
            @ApiImplicitParam(name = "id", required = true, value = "订单的编号")})
    public Result<Order> queryOrderById(@PathVariable("id") Long id) {
        return this.orderService.queryById(id);
    }

    @PostMapping("page")
    @ApiOperation(value = "分页查询当前用户订单，并且可以根据订单状态过滤", notes = "分页查询当前用户订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<PageResult<Order>> queryUserOrderList(OrderPageDTO dto) {
        return this.orderService.queryUserOrderList(dto);
    }

    @GetMapping("update/{id}/{status}")
    @ApiOperation(value = "更新订单状态", notes = "更新订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")
    })
    public Result<Boolean> updateStatus(@ApiParam(name = "id",value = "订单编号",required = true) @PathVariable("id") Long id,
                                        @ApiParam(name = "status",value = "订单状态：1未付款，2已付款未发货，3已发货未确认，4已确认未评价，5交易关闭，6交易成功，已评价",required = true) @PathVariable("status") Integer status) {
        return this.orderService.updateStatus(id, status);
    }

    @GetMapping("url/{id}")
    @ApiOperation(value = "生成微信扫码支付付款链接", notes = "生成付款链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单编号", type = "Long"),
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")
    })
    public Result<String> generateUrl(@PathVariable("id") Long orderId) {
        return this.payHelper.createPayUrl(orderId);
    }

    @GetMapping("state/{id}")
    @ApiOperation(value = "查询扫码支付付款状态,0, 未查询到支付信息 1,支付成功 2,支付失败", notes = "查询付款状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单编号", type = "Long"),
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")
    })
    public Result<Integer> queryPayState(@PathVariable("id") Long orderId) {
        return this.payHelper.queryOrder(orderId);
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "取消订单，多个以逗号隔开", notes = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")
    })
    public Result deleteOrder(@ApiParam(value = "订单id",name = "ids") @RequestParam(value = "ids",required = true) String ids) {
        return this.orderService.deleteOrder(ids);
    }

}
